<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户id" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="任务id" prop="taskId">
        <el-input
          v-model="queryParams.taskId"
          placeholder="请输入任务id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核状态" prop="auditStatus">
        <el-select v-model="queryParams.auditStatus" placeholder="请选择审核状态" clearable>
          <el-option
            v-for="dict in dict.type.task_audit_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:receiveTask:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="receiveTaskList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="用户id" align="center" prop="userId" />
      <el-table-column label="任务id" align="center" prop="taskId" />
      <el-table-column label="任务标题" align="center" prop="taskTitle" />
      <el-table-column label="审核状态" align="center" prop="auditStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.task_audit_status" :value="scope.row.auditStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="审核意见" align="center" prop="auditContent" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:receiveTask:edit']"
          >审核</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改接取任务关系对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户id" prop="userId">
          <el-input v-model="form.userId" :disabled='true' placeholder="请输入用户id" />
        </el-form-item>
        <el-form-item label="用户名称" prop="userName">
          <el-input v-model="form.userName" :disabled='true' />
        </el-form-item>
        <el-form-item label="任务id" prop="taskId">
          <el-input v-model="form.taskId" :disabled='true' />
        </el-form-item>
        <el-form-item label="任务标题" prop="taskTitle">
          <el-input v-model="form.taskTitle" :disabled='true' />
        </el-form-item>
        <el-form-item label="任务内容" prop="taskContent">
          <el-input v-model="form.taskContent" :disabled='true' type="textarea" />
        </el-form-item>
        <el-form-item label="审核意见">
          <el-input v-model="form.auditContent" type="textarea"/>
        </el-form-item>
        <el-divider content-position="center">任务提交文件表信息</el-divider>
        <el-table :data="receiveTaskFileList" :row-class-name="rowReceiveTaskFileIndex" @selection-change="handleReceiveTaskFileSelectionChange" ref="receiveTaskFile">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="提交任务文件" prop="url" width="550">
            <template slot-scope="scope">
              <image-preview :src="scope.row.url" placeholder="请输入提交任务文件" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm(3)">通过</el-button>
        <el-button @click="submitForm(4)">拒绝</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listReceiveTask, getReceiveTask, delReceiveTask, addReceiveTask, updateReceiveTask,auditTask } from "@/api/system/receiveTask";

export default {
  name: "ReceiveTask",
  dicts: ['task_audit_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedReceiveTaskFile: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 接取任务关系表格数据
      receiveTaskList: [],
      // 任务提交文件表表格数据
      receiveTaskFileList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        taskId: null,
        auditStatus: null,
        auditContent: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询接取任务关系列表 */
    getList() {
      this.loading = true;
      listReceiveTask(this.queryParams).then(response => {
        this.receiveTaskList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        taskId: null,
        auditStatus: null,
        auditContent: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.receiveTaskFileList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "任务审核";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getReceiveTask(id).then(response => {
        this.form = response.data;
        this.receiveTaskFileList = response.data.receiveTaskFileList;
        this.open = true;
        this.title = "任务审核";
      });
    },
    /** 提交按钮 */
    submitForm(status) {
      this.$refs["form"].validate(valid => {
            const data = {};
            data.id = this.form.id;
            data.auditStatus = status;
            data.taskId = this.form.taskId;
            data.auditContent = this.form.auditContent;
            auditTask(data).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除接取任务关系编号为"' + ids + '"的数据项？').then(function() {
        return delReceiveTask(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 任务提交文件表序号 */
    rowReceiveTaskFileIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 任务提交文件表添加按钮操作 */
    handleAddReceiveTaskFile() {
      let obj = {};
      obj.url = "";
      this.receiveTaskFileList.push(obj);
    },
    /** 任务提交文件表删除按钮操作 */
    handleDeleteReceiveTaskFile() {
      if (this.checkedReceiveTaskFile.length == 0) {
        this.$modal.msgError("请先选择要删除的任务提交文件表数据");
      } else {
        const receiveTaskFileList = this.receiveTaskFileList;
        const checkedReceiveTaskFile = this.checkedReceiveTaskFile;
        this.receiveTaskFileList = receiveTaskFileList.filter(function(item) {
          return checkedReceiveTaskFile.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleReceiveTaskFileSelectionChange(selection) {
      this.checkedReceiveTaskFile = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/receiveTask/export', {
        ...this.queryParams
      }, `receiveTask_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
